<template>
<div>

<van-nav-bar
  title="注册"
  left-text="返回"
  left-arrow
  @click-left="onClickLeft"
  @click-right="onClickRight"
/>


<van-form @submit="onSubmit">
  <van-field
    v-model="username"
    name="用户名"
    label="用户名"
    placeholder="用户名"
    :rules="[{ required: true, message: '请填写用户名' }]"
  />
    <van-field
    v-model="tel"
    name="手机号"
    label="手机号"
    placeholder="手机号"
    :rules="[{ required: true, message: '请填写手机号' }]"
  />
    <van-field
        v-model="password"
        required
        type="password"
        label="密码"
        placeholder="请输入密码"
    />
    <van-field
        v-model="password1"
        required
        type="password"
        label="确认密码"
        placeholder="请再次输入密码"
    />

    <van-field
        readonly
        clickable
        name="calendar"
        :value="creat_time"
        label="创建时间"
        placeholder="点击选择日期"
        @click="showCalendar = true"
        />
        <van-calendar v-model="showCalendar" @confirm="onConfirm" />

  <div style="margin: 16px;">
    <van-button round block type="info" native-type="submit">提交</van-button>
  </div>
</van-form>

</div>
</template>
<script>
 import { Toast } from 'vant';
export default {
   
  data() {
    return {
      name:'',
      username: '',
      tel: '',
      password: '',
      creat_time: '',
      showCalendar: false,
      showPicker: false,
      showPicker1: false,
      showPicker2: false,
      showPicker3: false,
      
    
    };
  },
  methods: {
    onSubmit(values){
        	if(this.username == null){
			Toast('注册失败！信息未完善');
            }else if(this.password != this.password1){
             Toast('密码输入两次不一致！');
            }else{
                this.$axios
                .post('user/regist',{
                    username: this.username,
                    tel: this.tel,
                    password: this.password,
                    creat_time: this.creat_time
                })
                .then(res=>{ //注册成功
                this.$router.push("/login");
                    if(res.data.code==1){
                        Toast.success('注册成功');
                        this.$router.push("/login");
                    }else if(res.data.code==2){
                        this.$router.push("/regist");
                        Toast.success('已有用户,注册失败');
                    }
                    
                })
                .catch(error=>{
                    console.log(error);
                });
        }
    },        
           
 
     onClickLeft() {
        this.$router.push('/login');
    },  
    onConfirm(date) {
      this.creat_time = `${date.getYear() + 1900}-${date.getMonth() + 1}-${date.getDate()}`;
      this.showCalendar = false;
    }, 
  }
}
</script>
